// packageA/pages/withdraw/withdraw.js
import { RedPacket } from '../../../models/redpacket'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

let packetModel = new RedPacket()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    amount: '',
    userName: '',
    cantake: 0,
    tapTime: '',
    show: true,
    shareType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCantake()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  withdraw() {
    var nowTime = new Date()
    if (nowTime - this.data.tapTime < 3000) {
      // 节流
      return
    }

    if (!this.data.userName) {
      wx.showToast({
        title: '请输入支付宝姓名',
        icon: 'none'
      })
      return
    }
    if (!this.data.account) {
      wx.showToast({
        title: '请输入支付宝账号',
        icon: 'none'
      })
      return
    }
    if (!this.data.amount) {
      wx.showToast({
        title: '请输入提现金额',
        icon: 'none'
      })
      return
    }
    if (this.data.amount == 0) {
      wx.showToast({
        title: '提现金额不能为0',
        icon: 'none'
      })
      return
    }
    if (this.data.amount > this.data.cantake) {
      wx.showToast({
        title: `最多可提现${this.data.cantake}`,
        icon: 'none'
      })
      return
    }
    packetModel.withdraw(
      {
        account: this.data.account,
        amount: this.data.amount,
        userName: this.data.userName
      },
      (res) => {
        Dialog.alert({
          title: '申请成功',
          message: '请等待审核通知'
        }).then(() => {
          wx.requestSubscribeMessage({
            tmplIds: ['-DHKrW9DRvbg3sEoVRYtcRxRyr-kYzyTN-9BNCY89rI'],
            complete(res) {
              wx.navigateBack()
            }
          })
        })
      }
    )
    this.setData({ tapTime: nowTime })
  },

  takeall() {
    this.setData({
      amount: this.data.cantake
    })
  },

  getCantake() {
    // 获取可提现金额
    packetModel.getMyPacket((res) => {
      this.setData({
        cantake: res.canWithdraw
      })
    })
  },

  closeTip() {
    this.setData({
      show: false
    })
  },

  shareFriends() {
    this.setData({
      shareType: 'friends'
    })
  },

  shareGroup() {
    this.setData({
      shareType: 'group'
    })
  },

  onShareAppMessage: function () {
    let path1 = `/pages/resume/resume?otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    let path2 = `/pages/candidate/candidate?otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    let img1 = 'https://m.xianmaiyangsheng.com/xcxImg/image/refund-share1.png'
    let img2 = 'https://m.xianmaiyangsheng.com/xcxImg/image/refund-share2.png'
    return {
      path: this.data.shareType == 'friends' ? path1 : path2,
      title:  this.data.shareType == 'friends' ? '分享赚佣金' : '技师找工作',
      imageUrl:  this.data.shareType == 'friends' ? img1 : img2,
    }
  }
})
